package cn.dyp.com.authoritymanagement.activity;

import java.util.ArrayList;
import java.util.List;


import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import cn.dyp.com.authoritymanagement.R;
import cn.dyp.com.authoritymanagement.adapter.MyAdapter;
import cn.dyp.com.authoritymanagement.entity.User;

/**
 * 程序启动后进入的Activity
 * 
 * @author asus
 *
 */

/*
Spinner提供了从一个数据集合中快速选择一项值的办法。默认情况下Spinner显示的是当前选择的值，
点击Spinner会弹出一个包含所有可选值的dropdown菜单，从该菜单中可以为Spinner选择一个新值。

要响应Spinner选择事件的,可以通过OnItemSelectedListener的回调方法实现

setDropDownViewResource设置展开的时候下拉菜单的样式
 */
public class MainActivity extends AppCompatActivity {
	private Spinner mRoleSpinner;
	private Spinner mUserSpinner;
	private EditText mUserET;
	private ImageView mPictureIV;
	private Button mOpenBtn;
	private String[] mRoles;
	private List<User> mUsers;
	private String currentRole;
	private MyAdapter mAdapter;
	private User mUser;
	private int index = 0;
	private int clickNum = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportActionBar().hide();
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		initData();
		initView();
		setSpinner();
	}

	/**
	 * 初始化数据
	 * 
	 */
	private void initData() {
		mUsers = new ArrayList<User>();
		mUsers.add(new User("admin", getString(R.string.manager)));
		mRoles = getResources().getStringArray(R.array.roles);
		currentRole = mRoles[0];
		mUser = new User();
	}

	/**
	 * 设置Spinner
	 * 
	 */
	private void setSpinner() {
		// 设置查看用户的Spinner
		mAdapter = new MyAdapter(this, mUsers);
		mUserSpinner.setAdapter(mAdapter);
		mUserSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				mUserET.setText(mUsers.get(position).getName());
				if (mRoles[0].equals(mUsers.get(position).getRole())) {
					mRoleSpinner.setSelection(0);
				} else {
					mRoleSpinner.setSelection(1);
				}
				defaultCase();
				index = position;
				clickNum = 0;
				getUser();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});
		// 设置权限选择的Spinner
		ArrayAdapter<String> roleAdapter = new ArrayAdapter<String>(this,
				R.layout.item, mRoles);
		mRoleSpinner.setAdapter(roleAdapter);
		mRoleSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				currentRole = mRoles[position];
				defaultCase();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});
	}

	/**
	 * 初始化控件
	 * 
	 */
	private void initView() {
		mRoleSpinner = (Spinner) findViewById(R.id.choose_role);
		mUserSpinner = (Spinner) findViewById(R.id.choose_user);
		mUserET = (EditText) findViewById(R.id.add_user);
		mPictureIV = (ImageView) findViewById(R.id.show);
		mOpenBtn = (Button) findViewById(R.id.open_btn);
	}

	/**
	 * 保存数据
	 * 
	 * @param v
	 *            视图
	 */
	public void saveData(View v) {
		String userString = mUserET.getText().toString().trim();
		User mUser = new User();
		if (userString.equals("")) {
			return;
		}
		mUser.setName(userString);
		mUser.setRole(currentRole);
		for (int i = 0; i < mUsers.size(); i++) {
			if (userString.equals(mUsers.get(i).getName())) {
				mUsers.remove(i);
			}
		}
		mUsers.add(mUser);
		getUser();
		mAdapter.notifyDataSetChanged();
	}

	/**
	 * 切换图片
	 * 
	 * @param v
	 *            视图
	 */
	public void pictureShow(View v) {
		authorityChoose(mUser.getRole());
	}

	/**
	 * 角色管理
	 * 
	 * @param role
	 *            角色
	 */
	private void authorityChoose(String role) {
		if (getString(R.string.manager).equals(role)) {
			if (clickNum % 2 == 0) {
				mPictureIV.setImageResource(R.drawable.open_icon);
				mOpenBtn.setBackgroundResource(R.drawable.close_btn);
			} else {
				mPictureIV.setImageResource(R.drawable.close_icon);
				mOpenBtn.setBackgroundResource(R.drawable.open_btn);
			}
			clickNum++;
		} else if (getString(R.string.general_user).equals(role)) {
			showAlertDialog(getString(R.string.prompt), getString(R.string.msg));
		}
	}

	/**
	 * 显示对话框
	 * 
	 * @param title
	 *            对话框标题
	 * @param message
	 *            对话框内容
	 */
	protected void showAlertDialog(String title, String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(getString(android.R.string.ok), null);
		builder.show();
	}

	/**
	 * 获取当前的User
	 * 
	 */
	private void getUser() {
		mUser = mUsers.get(index);
	}

	/**
	 * 默认情况下的控制状态
	 * 
	 */
	private void defaultCase() {
		mPictureIV.setImageResource(R.drawable.close_icon);
		mOpenBtn.setBackgroundResource(R.drawable.open_btn);
	}
}
